package com.mh.service.impl;

import com.mh.dto.R;
import com.mh.pojo.TypeInfos;
import com.mh.service.TypeInfosService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Date:2024/10/20
 * author:zmh
 * description: 商品类型服务类实现
 **/

@Slf4j
@Service
@RequiredArgsConstructor
public class TypeInfosServiceImpl implements TypeInfosService {

    private final MongoTemplate mongoTemplate;

    @Override
    public R<List<TypeInfos>> getHotTypeData() {
        // 构造条件
        Query query = new Query(Criteria.where("hotType").is(true));
        List<TypeInfos> typeInfos = mongoTemplate.find(query, TypeInfos.class);
        // 检查typeInfos的长度，仅保留前8条数据
        if (typeInfos.size() > 8) {
            typeInfos = typeInfos.subList(0, 8);
        }
        return R.success(typeInfos);
    }
}








